{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58dffa5e",
   "metadata": {},
   "source": [
    "#### Convert the original SHP dataset files to CSV format.\n",
    " - The original files are in HDF5 format.\n",
    " - Each file represents an ECG exam and has an array of shape 12 x L, where 12 is the number of leads and L is the number of samples.\n",
    " - The number of samples in each file ranges from 5000 to 30000 (records lasting 10 to 60 seconds at a sample frequency of 500 Hz)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dd8d21d",
   "metadata": {},
   "source": [
    "#### Import necessary libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d0a0dbd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import pandas as pd\n",
    "import csv\n",
    "import h5py\n",
    "import glob\n",
    "import os\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99f72c00",
   "metadata": {},
   "source": [
    "#### Generate the CSV file.\n",
    "- All 25770 ECGs was used.\n",
    "- Only the first 10 seconds of each ECG were used.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bed66805-bd01-445c-9b58-2a6b0a259e38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of available files: 25770\n",
      "\n",
      "Loading metadata...\n",
      "\n",
      "Start writing ecg_sph_dataset.csv file...\n",
      "\n",
      "100000 samples processed.\n",
      "200000 samples processed.\n",
      "300000 samples processed.\n",
      "400000 samples processed.\n",
      "500000 samples processed.\n",
      "600000 samples processed.\n",
      "700000 samples processed.\n",
      "800000 samples processed.\n",
      "900000 samples processed.\n",
      "1000000 samples processed.\n",
      "1100000 samples processed.\n",
      "1200000 samples processed.\n",
      "1300000 samples processed.\n",
      "1400000 samples processed.\n",
      "1500000 samples processed.\n",
      "1600000 samples processed.\n",
      "1700000 samples processed.\n",
      "1800000 samples processed.\n",
      "1900000 samples processed.\n",
      "2000000 samples processed.\n",
      "2100000 samples processed.\n",
      "2200000 samples processed.\n",
      "2300000 samples processed.\n",
      "2400000 samples processed.\n",
      "2500000 samples processed.\n",
      "2600000 samples processed.\n",
      "2700000 samples processed.\n",
      "2800000 samples processed.\n",
      "2900000 samples processed.\n",
      "3000000 samples processed.\n",
      "3100000 samples processed.\n",
      "3200000 samples processed.\n",
      "3300000 samples processed.\n",
      "3400000 samples processed.\n",
      "3500000 samples processed.\n",
      "3600000 samples processed.\n",
      "3700000 samples processed.\n",
      "3800000 samples processed.\n",
      "3900000 samples processed.\n",
      "4000000 samples processed.\n",
      "4100000 samples processed.\n",
      "4200000 samples processed.\n",
      "4300000 samples processed.\n",
      "4400000 samples processed.\n",
      "4500000 samples processed.\n",
      "4600000 samples processed.\n",
      "4700000 samples processed.\n",
      "4800000 samples processed.\n",
      "4900000 samples processed.\n",
      "5000000 samples processed.\n",
      "5100000 samples processed.\n",
      "5200000 samples processed.\n",
      "5300000 samples processed.\n",
      "5400000 samples processed.\n",
      "5500000 samples processed.\n",
      "5600000 samples processed.\n",
      "5700000 samples processed.\n",
      "5800000 samples processed.\n",
      "5900000 samples processed.\n",
      "6000000 samples processed.\n",
      "6100000 samples processed.\n",
      "6200000 samples processed.\n",
      "6300000 samples processed.\n",
      "6400000 samples processed.\n",
      "6500000 samples processed.\n",
      "6600000 samples processed.\n",
      "6700000 samples processed.\n",
      "6800000 samples processed.\n",
      "6900000 samples processed.\n",
      "7000000 samples processed.\n",
      "7100000 samples processed.\n",
      "7200000 samples processed.\n",
      "7300000 samples processed.\n",
      "7400000 samples processed.\n",
      "7500000 samples processed.\n",
      "7600000 samples processed.\n",
      "7700000 samples processed.\n",
      "7800000 samples processed.\n",
      "7900000 samples processed.\n",
      "8000000 samples processed.\n",
      "8100000 samples processed.\n",
      "8200000 samples processed.\n",
      "8300000 samples processed.\n",
      "8400000 samples processed.\n",
      "8500000 samples processed.\n",
      "8600000 samples processed.\n",
      "8700000 samples processed.\n",
      "8800000 samples processed.\n",
      "8900000 samples processed.\n",
      "9000000 samples processed.\n",
      "9100000 samples processed.\n",
      "9200000 samples processed.\n",
      "9300000 samples processed.\n",
      "9400000 samples processed.\n",
      "9500000 samples processed.\n",
      "9600000 samples processed.\n",
      "9700000 samples processed.\n",
      "9800000 samples processed.\n",
      "9900000 samples processed.\n",
      "10000000 samples processed.\n",
      "10100000 samples processed.\n",
      "10200000 samples processed.\n",
      "10300000 samples processed.\n",
      "10400000 samples processed.\n",
      "10500000 samples processed.\n",
      "10600000 samples processed.\n",
      "10700000 samples processed.\n",
      "10800000 samples processed.\n",
      "10900000 samples processed.\n",
      "11000000 samples processed.\n",
      "11100000 samples processed.\n",
      "11200000 samples processed.\n",
      "11300000 samples processed.\n",
      "11400000 samples processed.\n",
      "11500000 samples processed.\n",
      "11600000 samples processed.\n",
      "11700000 samples processed.\n",
      "11800000 samples processed.\n",
      "11900000 samples processed.\n",
      "12000000 samples processed.\n",
      "12100000 samples processed.\n",
      "12200000 samples processed.\n",
      "12300000 samples processed.\n",
      "12400000 samples processed.\n",
      "12500000 samples processed.\n",
      "12600000 samples processed.\n",
      "12700000 samples processed.\n",
      "12800000 samples processed.\n",
      "12900000 samples processed.\n",
      "13000000 samples processed.\n",
      "13100000 samples processed.\n",
      "13200000 samples processed.\n",
      "13300000 samples processed.\n",
      "13400000 samples processed.\n",
      "13500000 samples processed.\n",
      "13600000 samples processed.\n",
      "13700000 samples processed.\n",
      "13800000 samples processed.\n",
      "13900000 samples processed.\n",
      "14000000 samples processed.\n",
      "14100000 samples processed.\n",
      "14200000 samples processed.\n",
      "14300000 samples processed.\n",
      "14400000 samples processed.\n",
      "14500000 samples processed.\n",
      "14600000 samples processed.\n",
      "14700000 samples processed.\n",
      "14800000 samples processed.\n",
      "14900000 samples processed.\n",
      "15000000 samples processed.\n",
      "15100000 samples processed.\n",
      "15200000 samples processed.\n",
      "15300000 samples processed.\n",
      "15400000 samples processed.\n",
      "15500000 samples processed.\n",
      "15600000 samples processed.\n",
      "15700000 samples processed.\n",
      "15800000 samples processed.\n",
      "15900000 samples processed.\n",
      "16000000 samples processed.\n",
      "16100000 samples processed.\n",
      "16200000 samples processed.\n",
      "16300000 samples processed.\n",
      "16400000 samples processed.\n",
      "16500000 samples processed.\n",
      "16600000 samples processed.\n",
      "16700000 samples processed.\n",
      "16800000 samples processed.\n",
      "16900000 samples processed.\n",
      "17000000 samples processed.\n",
      "17100000 samples processed.\n",
      "17200000 samples processed.\n",
      "17300000 samples processed.\n",
      "17400000 samples processed.\n",
      "17500000 samples processed.\n",
      "17600000 samples processed.\n",
      "17700000 samples processed.\n",
      "17800000 samples processed.\n",
      "17900000 samples processed.\n",
      "18000000 samples processed.\n",
      "18100000 samples processed.\n",
      "18200000 samples processed.\n",
      "18300000 samples processed.\n",
      "18400000 samples processed.\n",
      "18500000 samples processed.\n",
      "18600000 samples processed.\n",
      "18700000 samples processed.\n",
      "18800000 samples processed.\n",
      "18900000 samples processed.\n",
      "19000000 samples processed.\n",
      "19100000 samples processed.\n",
      "19200000 samples processed.\n",
      "19300000 samples processed.\n",
      "19400000 samples processed.\n",
      "19500000 samples processed.\n",
      "19600000 samples processed.\n",
      "19700000 samples processed.\n",
      "19800000 samples processed.\n",
      "19900000 samples processed.\n",
      "20000000 samples processed.\n",
      "20100000 samples processed.\n",
      "20200000 samples processed.\n",
      "20300000 samples processed.\n",
      "20400000 samples processed.\n",
      "20500000 samples processed.\n",
      "20600000 samples processed.\n",
      "20700000 samples processed.\n",
      "20800000 samples processed.\n",
      "20900000 samples processed.\n",
      "21000000 samples processed.\n",
      "21100000 samples processed.\n",
      "21200000 samples processed.\n",
      "21300000 samples processed.\n",
      "21400000 samples processed.\n",
      "21500000 samples processed.\n",
      "21600000 samples processed.\n",
      "21700000 samples processed.\n",
      "21800000 samples processed.\n",
      "21900000 samples processed.\n",
      "22000000 samples processed.\n",
      "22100000 samples processed.\n",
      "22200000 samples processed.\n",
      "22300000 samples processed.\n",
      "22400000 samples processed.\n",
      "22500000 samples processed.\n",
      "22600000 samples processed.\n",
      "22700000 samples processed.\n",
      "22800000 samples processed.\n",
      "22900000 samples processed.\n",
      "23000000 samples processed.\n",
      "23100000 samples processed.\n",
      "23200000 samples processed.\n",
      "23300000 samples processed.\n",
      "23400000 samples processed.\n",
      "23500000 samples processed.\n",
      "23600000 samples processed.\n",
      "23700000 samples processed.\n",
      "23800000 samples processed.\n",
      "23900000 samples processed.\n",
      "24000000 samples processed.\n",
      "24100000 samples processed.\n",
      "24200000 samples processed.\n",
      "24300000 samples processed.\n",
      "24400000 samples processed.\n",
      "24500000 samples processed.\n",
      "24600000 samples processed.\n",
      "24700000 samples processed.\n",
      "24800000 samples processed.\n",
      "24900000 samples processed.\n",
      "25000000 samples processed.\n",
      "25100000 samples processed.\n",
      "25200000 samples processed.\n",
      "25300000 samples processed.\n",
      "25400000 samples processed.\n",
      "25500000 samples processed.\n",
      "25600000 samples processed.\n",
      "25700000 samples processed.\n",
      "25800000 samples processed.\n",
      "25900000 samples processed.\n",
      "26000000 samples processed.\n",
      "26100000 samples processed.\n",
      "26200000 samples processed.\n",
      "26300000 samples processed.\n",
      "26400000 samples processed.\n",
      "26500000 samples processed.\n",
      "26600000 samples processed.\n",
      "26700000 samples processed.\n",
      "26800000 samples processed.\n",
      "26900000 samples processed.\n",
      "27000000 samples processed.\n",
      "27100000 samples processed.\n",
      "27200000 samples processed.\n",
      "27300000 samples processed.\n",
      "27400000 samples processed.\n",
      "27500000 samples processed.\n",
      "27600000 samples processed.\n",
      "27700000 samples processed.\n",
      "27800000 samples processed.\n",
      "27900000 samples processed.\n",
      "28000000 samples processed.\n",
      "28100000 samples processed.\n",
      "28200000 samples processed.\n",
      "28300000 samples processed.\n",
      "28400000 samples processed.\n",
      "28500000 samples processed.\n",
      "28600000 samples processed.\n",
      "28700000 samples processed.\n",
      "28800000 samples processed.\n",
      "28900000 samples processed.\n",
      "29000000 samples processed.\n",
      "29100000 samples processed.\n",
      "29200000 samples processed.\n",
      "29300000 samples processed.\n",
      "29400000 samples processed.\n",
      "29500000 samples processed.\n",
      "29600000 samples processed.\n",
      "29700000 samples processed.\n",
      "29800000 samples processed.\n",
      "29900000 samples processed.\n",
      "30000000 samples processed.\n",
      "30100000 samples processed.\n",
      "30200000 samples processed.\n",
      "30300000 samples processed.\n",
      "30400000 samples processed.\n",
      "30500000 samples processed.\n",
      "30600000 samples processed.\n",
      "30700000 samples processed.\n",
      "30800000 samples processed.\n",
      "30900000 samples processed.\n",
      "31000000 samples processed.\n",
      "31100000 samples processed.\n",
      "31200000 samples processed.\n",
      "31300000 samples processed.\n",
      "31400000 samples processed.\n",
      "31500000 samples processed.\n",
      "31600000 samples processed.\n",
      "31700000 samples processed.\n",
      "31800000 samples processed.\n",
      "31900000 samples processed.\n",
      "32000000 samples processed.\n",
      "32100000 samples processed.\n",
      "32200000 samples processed.\n",
      "32300000 samples processed.\n",
      "32400000 samples processed.\n",
      "32500000 samples processed.\n",
      "32600000 samples processed.\n",
      "32700000 samples processed.\n",
      "32800000 samples processed.\n",
      "32900000 samples processed.\n",
      "33000000 samples processed.\n",
      "33100000 samples processed.\n",
      "33200000 samples processed.\n",
      "33300000 samples processed.\n",
      "33400000 samples processed.\n",
      "33500000 samples processed.\n",
      "33600000 samples processed.\n",
      "33700000 samples processed.\n",
      "33800000 samples processed.\n",
      "33900000 samples processed.\n",
      "34000000 samples processed.\n",
      "34100000 samples processed.\n",
      "34200000 samples processed.\n",
      "34300000 samples processed.\n",
      "34400000 samples processed.\n",
      "34500000 samples processed.\n",
      "34600000 samples processed.\n",
      "34700000 samples processed.\n",
      "34800000 samples processed.\n",
      "34900000 samples processed.\n",
      "35000000 samples processed.\n",
      "35100000 samples processed.\n",
      "35200000 samples processed.\n",
      "35300000 samples processed.\n",
      "35400000 samples processed.\n",
      "35500000 samples processed.\n",
      "35600000 samples processed.\n",
      "35700000 samples processed.\n",
      "35800000 samples processed.\n",
      "35900000 samples processed.\n",
      "36000000 samples processed.\n",
      "36100000 samples processed.\n",
      "36200000 samples processed.\n",
      "36300000 samples processed.\n",
      "36400000 samples processed.\n",
      "36500000 samples processed.\n",
      "36600000 samples processed.\n",
      "36700000 samples processed.\n",
      "36800000 samples processed.\n",
      "36900000 samples processed.\n",
      "37000000 samples processed.\n",
      "37100000 samples processed.\n",
      "37200000 samples processed.\n",
      "37300000 samples processed.\n",
      "37400000 samples processed.\n",
      "37500000 samples processed.\n",
      "37600000 samples processed.\n",
      "37700000 samples processed.\n",
      "37800000 samples processed.\n",
      "37900000 samples processed.\n",
      "38000000 samples processed.\n",
      "38100000 samples processed.\n",
      "38200000 samples processed.\n",
      "38300000 samples processed.\n",
      "38400000 samples processed.\n",
      "38500000 samples processed.\n",
      "38600000 samples processed.\n",
      "38700000 samples processed.\n",
      "38800000 samples processed.\n",
      "38900000 samples processed.\n",
      "39000000 samples processed.\n",
      "39100000 samples processed.\n",
      "39200000 samples processed.\n",
      "39300000 samples processed.\n",
      "39400000 samples processed.\n",
      "39500000 samples processed.\n",
      "39600000 samples processed.\n",
      "39700000 samples processed.\n",
      "39800000 samples processed.\n",
      "39900000 samples processed.\n",
      "40000000 samples processed.\n",
      "40100000 samples processed.\n",
      "40200000 samples processed.\n",
      "40300000 samples processed.\n",
      "40400000 samples processed.\n",
      "40500000 samples processed.\n",
      "40600000 samples processed.\n",
      "40700000 samples processed.\n",
      "40800000 samples processed.\n",
      "40900000 samples processed.\n",
      "41000000 samples processed.\n",
      "41100000 samples processed.\n",
      "41200000 samples processed.\n",
      "41300000 samples processed.\n",
      "41400000 samples processed.\n",
      "41500000 samples processed.\n",
      "41600000 samples processed.\n",
      "41700000 samples processed.\n",
      "41800000 samples processed.\n",
      "41900000 samples processed.\n",
      "42000000 samples processed.\n",
      "42100000 samples processed.\n",
      "42200000 samples processed.\n",
      "42300000 samples processed.\n",
      "42400000 samples processed.\n",
      "42500000 samples processed.\n",
      "42600000 samples processed.\n",
      "42700000 samples processed.\n",
      "42800000 samples processed.\n",
      "42900000 samples processed.\n",
      "43000000 samples processed.\n",
      "43100000 samples processed.\n",
      "43200000 samples processed.\n",
      "43300000 samples processed.\n",
      "43400000 samples processed.\n",
      "43500000 samples processed.\n",
      "43600000 samples processed.\n",
      "43700000 samples processed.\n",
      "43800000 samples processed.\n",
      "43900000 samples processed.\n",
      "44000000 samples processed.\n",
      "44100000 samples processed.\n",
      "44200000 samples processed.\n",
      "44300000 samples processed.\n",
      "44400000 samples processed.\n",
      "44500000 samples processed.\n",
      "44600000 samples processed.\n",
      "44700000 samples processed.\n",
      "44800000 samples processed.\n",
      "44900000 samples processed.\n",
      "45000000 samples processed.\n",
      "45100000 samples processed.\n",
      "45200000 samples processed.\n",
      "45300000 samples processed.\n",
      "45400000 samples processed.\n",
      "45500000 samples processed.\n",
      "45600000 samples processed.\n",
      "45700000 samples processed.\n",
      "45800000 samples processed.\n",
      "45900000 samples processed.\n",
      "46000000 samples processed.\n",
      "46100000 samples processed.\n",
      "46200000 samples processed.\n",
      "46300000 samples processed.\n",
      "46400000 samples processed.\n",
      "46500000 samples processed.\n",
      "46600000 samples processed.\n",
      "46700000 samples processed.\n",
      "46800000 samples processed.\n",
      "46900000 samples processed.\n",
      "47000000 samples processed.\n",
      "47100000 samples processed.\n",
      "47200000 samples processed.\n",
      "47300000 samples processed.\n",
      "47400000 samples processed.\n",
      "47500000 samples processed.\n",
      "47600000 samples processed.\n",
      "47700000 samples processed.\n",
      "47800000 samples processed.\n",
      "47900000 samples processed.\n",
      "48000000 samples processed.\n",
      "48100000 samples processed.\n",
      "48200000 samples processed.\n",
      "48300000 samples processed.\n",
      "48400000 samples processed.\n",
      "48500000 samples processed.\n",
      "48600000 samples processed.\n",
      "48700000 samples processed.\n",
      "48800000 samples processed.\n",
      "48900000 samples processed.\n",
      "49000000 samples processed.\n",
      "49100000 samples processed.\n",
      "49200000 samples processed.\n",
      "49300000 samples processed.\n",
      "49400000 samples processed.\n",
      "49500000 samples processed.\n",
      "49600000 samples processed.\n",
      "49700000 samples processed.\n",
      "49800000 samples processed.\n",
      "49900000 samples processed.\n",
      "50000000 samples processed.\n",
      "50100000 samples processed.\n",
      "50200000 samples processed.\n",
      "50300000 samples processed.\n",
      "50400000 samples processed.\n",
      "50500000 samples processed.\n",
      "50600000 samples processed.\n",
      "50700000 samples processed.\n",
      "50800000 samples processed.\n",
      "50900000 samples processed.\n",
      "51000000 samples processed.\n",
      "51100000 samples processed.\n",
      "51200000 samples processed.\n",
      "51300000 samples processed.\n",
      "51400000 samples processed.\n",
      "51500000 samples processed.\n",
      "51600000 samples processed.\n",
      "51700000 samples processed.\n",
      "51800000 samples processed.\n",
      "51900000 samples processed.\n",
      "52000000 samples processed.\n",
      "52100000 samples processed.\n",
      "52200000 samples processed.\n",
      "52300000 samples processed.\n",
      "52400000 samples processed.\n",
      "52500000 samples processed.\n",
      "52600000 samples processed.\n",
      "52700000 samples processed.\n",
      "52800000 samples processed.\n",
      "52900000 samples processed.\n",
      "53000000 samples processed.\n",
      "53100000 samples processed.\n",
      "53200000 samples processed.\n",
      "53300000 samples processed.\n",
      "53400000 samples processed.\n",
      "53500000 samples processed.\n",
      "53600000 samples processed.\n",
      "53700000 samples processed.\n",
      "53800000 samples processed.\n",
      "53900000 samples processed.\n",
      "54000000 samples processed.\n",
      "54100000 samples processed.\n",
      "54200000 samples processed.\n",
      "54300000 samples processed.\n",
      "54400000 samples processed.\n",
      "54500000 samples processed.\n",
      "54600000 samples processed.\n",
      "54700000 samples processed.\n",
      "54800000 samples processed.\n",
      "54900000 samples processed.\n",
      "55000000 samples processed.\n",
      "55100000 samples processed.\n",
      "55200000 samples processed.\n",
      "55300000 samples processed.\n",
      "55400000 samples processed.\n",
      "55500000 samples processed.\n",
      "55600000 samples processed.\n",
      "55700000 samples processed.\n",
      "55800000 samples processed.\n",
      "55900000 samples processed.\n",
      "56000000 samples processed.\n",
      "56100000 samples processed.\n",
      "56200000 samples processed.\n",
      "56300000 samples processed.\n",
      "56400000 samples processed.\n",
      "56500000 samples processed.\n",
      "56600000 samples processed.\n",
      "56700000 samples processed.\n",
      "56800000 samples processed.\n",
      "56900000 samples processed.\n",
      "57000000 samples processed.\n",
      "57100000 samples processed.\n",
      "57200000 samples processed.\n",
      "57300000 samples processed.\n",
      "57400000 samples processed.\n",
      "57500000 samples processed.\n",
      "57600000 samples processed.\n",
      "57700000 samples processed.\n",
      "57800000 samples processed.\n",
      "57900000 samples processed.\n",
      "58000000 samples processed.\n",
      "58100000 samples processed.\n",
      "58200000 samples processed.\n",
      "58300000 samples processed.\n",
      "58400000 samples processed.\n",
      "58500000 samples processed.\n",
      "58600000 samples processed.\n",
      "58700000 samples processed.\n",
      "58800000 samples processed.\n",
      "58900000 samples processed.\n",
      "59000000 samples processed.\n",
      "59100000 samples processed.\n",
      "59200000 samples processed.\n",
      "59300000 samples processed.\n",
      "59400000 samples processed.\n",
      "59500000 samples processed.\n",
      "59600000 samples processed.\n",
      "59700000 samples processed.\n",
      "59800000 samples processed.\n",
      "59900000 samples processed.\n",
      "60000000 samples processed.\n",
      "60100000 samples processed.\n",
      "60200000 samples processed.\n",
      "60300000 samples processed.\n",
      "60400000 samples processed.\n",
      "60500000 samples processed.\n",
      "60600000 samples processed.\n",
      "60700000 samples processed.\n",
      "60800000 samples processed.\n",
      "60900000 samples processed.\n",
      "61000000 samples processed.\n",
      "61100000 samples processed.\n",
      "61200000 samples processed.\n",
      "61300000 samples processed.\n",
      "61400000 samples processed.\n",
      "61500000 samples processed.\n",
      "61600000 samples processed.\n",
      "61700000 samples processed.\n",
      "61800000 samples processed.\n",
      "61900000 samples processed.\n",
      "62000000 samples processed.\n",
      "62100000 samples processed.\n",
      "62200000 samples processed.\n",
      "62300000 samples processed.\n",
      "62400000 samples processed.\n",
      "62500000 samples processed.\n",
      "62600000 samples processed.\n",
      "62700000 samples processed.\n",
      "62800000 samples processed.\n",
      "62900000 samples processed.\n",
      "63000000 samples processed.\n",
      "63100000 samples processed.\n",
      "63200000 samples processed.\n",
      "63300000 samples processed.\n",
      "63400000 samples processed.\n",
      "63500000 samples processed.\n",
      "63600000 samples processed.\n",
      "63700000 samples processed.\n",
      "63800000 samples processed.\n",
      "63900000 samples processed.\n",
      "64000000 samples processed.\n",
      "64100000 samples processed.\n",
      "64200000 samples processed.\n",
      "64300000 samples processed.\n",
      "64400000 samples processed.\n",
      "64500000 samples processed.\n",
      "64600000 samples processed.\n",
      "64700000 samples processed.\n",
      "64800000 samples processed.\n",
      "64900000 samples processed.\n",
      "65000000 samples processed.\n",
      "65100000 samples processed.\n",
      "65200000 samples processed.\n",
      "65300000 samples processed.\n",
      "65400000 samples processed.\n",
      "65500000 samples processed.\n",
      "65600000 samples processed.\n",
      "65700000 samples processed.\n",
      "65800000 samples processed.\n",
      "65900000 samples processed.\n",
      "66000000 samples processed.\n",
      "66100000 samples processed.\n",
      "66200000 samples processed.\n",
      "66300000 samples processed.\n",
      "66400000 samples processed.\n",
      "66500000 samples processed.\n",
      "66600000 samples processed.\n",
      "66700000 samples processed.\n",
      "66800000 samples processed.\n",
      "66900000 samples processed.\n",
      "67000000 samples processed.\n",
      "67100000 samples processed.\n",
      "67200000 samples processed.\n",
      "67300000 samples processed.\n",
      "67400000 samples processed.\n",
      "67500000 samples processed.\n",
      "67600000 samples processed.\n",
      "67700000 samples processed.\n",
      "67800000 samples processed.\n",
      "67900000 samples processed.\n",
      "68000000 samples processed.\n",
      "68100000 samples processed.\n",
      "68200000 samples processed.\n",
      "68300000 samples processed.\n",
      "68400000 samples processed.\n",
      "68500000 samples processed.\n",
      "68600000 samples processed.\n",
      "68700000 samples processed.\n",
      "68800000 samples processed.\n",
      "68900000 samples processed.\n",
      "69000000 samples processed.\n",
      "69100000 samples processed.\n",
      "69200000 samples processed.\n",
      "69300000 samples processed.\n",
      "69400000 samples processed.\n",
      "69500000 samples processed.\n",
      "69600000 samples processed.\n",
      "69700000 samples processed.\n",
      "69800000 samples processed.\n",
      "69900000 samples processed.\n",
      "70000000 samples processed.\n",
      "70100000 samples processed.\n",
      "70200000 samples processed.\n",
      "70300000 samples processed.\n",
      "70400000 samples processed.\n",
      "70500000 samples processed.\n",
      "70600000 samples processed.\n",
      "70700000 samples processed.\n",
      "70800000 samples processed.\n",
      "70900000 samples processed.\n",
      "71000000 samples processed.\n",
      "71100000 samples processed.\n",
      "71200000 samples processed.\n",
      "71300000 samples processed.\n",
      "71400000 samples processed.\n",
      "71500000 samples processed.\n",
      "71600000 samples processed.\n",
      "71700000 samples processed.\n",
      "71800000 samples processed.\n",
      "71900000 samples processed.\n",
      "72000000 samples processed.\n",
      "72100000 samples processed.\n",
      "72200000 samples processed.\n",
      "72300000 samples processed.\n",
      "72400000 samples processed.\n",
      "72500000 samples processed.\n",
      "72600000 samples processed.\n",
      "72700000 samples processed.\n",
      "72800000 samples processed.\n",
      "72900000 samples processed.\n",
      "73000000 samples processed.\n",
      "73100000 samples processed.\n",
      "73200000 samples processed.\n",
      "73300000 samples processed.\n",
      "73400000 samples processed.\n",
      "73500000 samples processed.\n",
      "73600000 samples processed.\n",
      "73700000 samples processed.\n",
      "73800000 samples processed.\n",
      "73900000 samples processed.\n",
      "74000000 samples processed.\n",
      "74100000 samples processed.\n",
      "74200000 samples processed.\n",
      "74300000 samples processed.\n",
      "74400000 samples processed.\n",
      "74500000 samples processed.\n",
      "74600000 samples processed.\n",
      "74700000 samples processed.\n",
      "74800000 samples processed.\n",
      "74900000 samples processed.\n",
      "75000000 samples processed.\n",
      "75100000 samples processed.\n",
      "75200000 samples processed.\n",
      "75300000 samples processed.\n",
      "75400000 samples processed.\n",
      "75500000 samples processed.\n",
      "75600000 samples processed.\n",
      "75700000 samples processed.\n",
      "75800000 samples processed.\n",
      "75900000 samples processed.\n",
      "76000000 samples processed.\n",
      "76100000 samples processed.\n",
      "76200000 samples processed.\n",
      "76300000 samples processed.\n",
      "76400000 samples processed.\n",
      "76500000 samples processed.\n",
      "76600000 samples processed.\n",
      "76700000 samples processed.\n",
      "76800000 samples processed.\n",
      "76900000 samples processed.\n",
      "77000000 samples processed.\n",
      "77100000 samples processed.\n",
      "77200000 samples processed.\n",
      "77300000 samples processed.\n",
      "77400000 samples processed.\n",
      "77500000 samples processed.\n",
      "77600000 samples processed.\n",
      "77700000 samples processed.\n",
      "77800000 samples processed.\n",
      "77900000 samples processed.\n",
      "78000000 samples processed.\n",
      "78100000 samples processed.\n",
      "78200000 samples processed.\n",
      "78300000 samples processed.\n",
      "78400000 samples processed.\n",
      "78500000 samples processed.\n",
      "78600000 samples processed.\n",
      "78700000 samples processed.\n",
      "78800000 samples processed.\n",
      "78900000 samples processed.\n",
      "79000000 samples processed.\n",
      "79100000 samples processed.\n",
      "79200000 samples processed.\n",
      "79300000 samples processed.\n",
      "79400000 samples processed.\n",
      "79500000 samples processed.\n",
      "79600000 samples processed.\n",
      "79700000 samples processed.\n",
      "79800000 samples processed.\n",
      "79900000 samples processed.\n",
      "80000000 samples processed.\n",
      "80100000 samples processed.\n",
      "80200000 samples processed.\n",
      "80300000 samples processed.\n",
      "80400000 samples processed.\n",
      "80500000 samples processed.\n",
      "80600000 samples processed.\n",
      "80700000 samples processed.\n",
      "80800000 samples processed.\n",
      "80900000 samples processed.\n",
      "81000000 samples processed.\n",
      "81100000 samples processed.\n",
      "81200000 samples processed.\n",
      "81300000 samples processed.\n",
      "81400000 samples processed.\n",
      "81500000 samples processed.\n",
      "81600000 samples processed.\n",
      "81700000 samples processed.\n",
      "81800000 samples processed.\n",
      "81900000 samples processed.\n",
      "82000000 samples processed.\n",
      "82100000 samples processed.\n",
      "82200000 samples processed.\n",
      "82300000 samples processed.\n",
      "82400000 samples processed.\n",
      "82500000 samples processed.\n",
      "82600000 samples processed.\n",
      "82700000 samples processed.\n",
      "82800000 samples processed.\n",
      "82900000 samples processed.\n",
      "83000000 samples processed.\n",
      "83100000 samples processed.\n",
      "83200000 samples processed.\n",
      "83300000 samples processed.\n",
      "83400000 samples processed.\n",
      "83500000 samples processed.\n",
      "83600000 samples processed.\n",
      "83700000 samples processed.\n",
      "83800000 samples processed.\n",
      "83900000 samples processed.\n",
      "84000000 samples processed.\n",
      "84100000 samples processed.\n",
      "84200000 samples processed.\n",
      "84300000 samples processed.\n",
      "84400000 samples processed.\n",
      "84500000 samples processed.\n",
      "84600000 samples processed.\n",
      "84700000 samples processed.\n",
      "84800000 samples processed.\n",
      "84900000 samples processed.\n",
      "85000000 samples processed.\n",
      "85100000 samples processed.\n",
      "85200000 samples processed.\n",
      "85300000 samples processed.\n",
      "85400000 samples processed.\n",
      "85500000 samples processed.\n",
      "85600000 samples processed.\n",
      "85700000 samples processed.\n",
      "85800000 samples processed.\n",
      "85900000 samples processed.\n",
      "86000000 samples processed.\n",
      "86100000 samples processed.\n",
      "86200000 samples processed.\n",
      "86300000 samples processed.\n",
      "86400000 samples processed.\n",
      "86500000 samples processed.\n",
      "86600000 samples processed.\n",
      "86700000 samples processed.\n",
      "86800000 samples processed.\n",
      "86900000 samples processed.\n",
      "87000000 samples processed.\n",
      "87100000 samples processed.\n",
      "87200000 samples processed.\n",
      "87300000 samples processed.\n",
      "87400000 samples processed.\n",
      "87500000 samples processed.\n",
      "87600000 samples processed.\n",
      "87700000 samples processed.\n",
      "87800000 samples processed.\n",
      "87900000 samples processed.\n",
      "88000000 samples processed.\n",
      "88100000 samples processed.\n",
      "88200000 samples processed.\n",
      "88300000 samples processed.\n",
      "88400000 samples processed.\n",
      "88500000 samples processed.\n",
      "88600000 samples processed.\n",
      "88700000 samples processed.\n",
      "88800000 samples processed.\n",
      "88900000 samples processed.\n",
      "89000000 samples processed.\n",
      "89100000 samples processed.\n",
      "89200000 samples processed.\n",
      "89300000 samples processed.\n",
      "89400000 samples processed.\n",
      "89500000 samples processed.\n",
      "89600000 samples processed.\n",
      "89700000 samples processed.\n",
      "89800000 samples processed.\n",
      "89900000 samples processed.\n",
      "90000000 samples processed.\n",
      "90100000 samples processed.\n",
      "90200000 samples processed.\n",
      "90300000 samples processed.\n",
      "90400000 samples processed.\n",
      "90500000 samples processed.\n",
      "90600000 samples processed.\n",
      "90700000 samples processed.\n",
      "90800000 samples processed.\n",
      "90900000 samples processed.\n",
      "91000000 samples processed.\n",
      "91100000 samples processed.\n",
      "91200000 samples processed.\n",
      "91300000 samples processed.\n",
      "91400000 samples processed.\n",
      "91500000 samples processed.\n",
      "91600000 samples processed.\n",
      "91700000 samples processed.\n",
      "91800000 samples processed.\n",
      "91900000 samples processed.\n",
      "92000000 samples processed.\n",
      "92100000 samples processed.\n",
      "92200000 samples processed.\n",
      "92300000 samples processed.\n",
      "92400000 samples processed.\n",
      "92500000 samples processed.\n",
      "92600000 samples processed.\n",
      "92700000 samples processed.\n",
      "92800000 samples processed.\n",
      "92900000 samples processed.\n",
      "93000000 samples processed.\n",
      "93100000 samples processed.\n",
      "93200000 samples processed.\n",
      "93300000 samples processed.\n",
      "93400000 samples processed.\n",
      "93500000 samples processed.\n",
      "93600000 samples processed.\n",
      "93700000 samples processed.\n",
      "93800000 samples processed.\n",
      "93900000 samples processed.\n",
      "94000000 samples processed.\n",
      "94100000 samples processed.\n",
      "94200000 samples processed.\n",
      "94300000 samples processed.\n",
      "94400000 samples processed.\n",
      "94500000 samples processed.\n",
      "94600000 samples processed.\n",
      "94700000 samples processed.\n",
      "94800000 samples processed.\n",
      "94900000 samples processed.\n",
      "95000000 samples processed.\n",
      "95100000 samples processed.\n",
      "95200000 samples processed.\n",
      "95300000 samples processed.\n",
      "95400000 samples processed.\n",
      "95500000 samples processed.\n",
      "95600000 samples processed.\n",
      "95700000 samples processed.\n",
      "95800000 samples processed.\n",
      "95900000 samples processed.\n",
      "96000000 samples processed.\n",
      "96100000 samples processed.\n",
      "96200000 samples processed.\n",
      "96300000 samples processed.\n",
      "96400000 samples processed.\n",
      "96500000 samples processed.\n",
      "96600000 samples processed.\n",
      "96700000 samples processed.\n",
      "96800000 samples processed.\n",
      "96900000 samples processed.\n",
      "97000000 samples processed.\n",
      "97100000 samples processed.\n",
      "97200000 samples processed.\n",
      "97300000 samples processed.\n",
      "97400000 samples processed.\n",
      "97500000 samples processed.\n",
      "97600000 samples processed.\n",
      "97700000 samples processed.\n",
      "97800000 samples processed.\n",
      "97900000 samples processed.\n",
      "98000000 samples processed.\n",
      "98100000 samples processed.\n",
      "98200000 samples processed.\n",
      "98300000 samples processed.\n",
      "98400000 samples processed.\n",
      "98500000 samples processed.\n",
      "98600000 samples processed.\n",
      "98700000 samples processed.\n",
      "98800000 samples processed.\n",
      "98900000 samples processed.\n",
      "99000000 samples processed.\n",
      "99100000 samples processed.\n",
      "99200000 samples processed.\n",
      "99300000 samples processed.\n",
      "99400000 samples processed.\n",
      "99500000 samples processed.\n",
      "99600000 samples processed.\n",
      "99700000 samples processed.\n",
      "99800000 samples processed.\n",
      "99900000 samples processed.\n",
      "100000000 samples processed.\n",
      "100100000 samples processed.\n",
      "100200000 samples processed.\n",
      "100300000 samples processed.\n",
      "100400000 samples processed.\n",
      "100500000 samples processed.\n",
      "100600000 samples processed.\n",
      "100700000 samples processed.\n",
      "100800000 samples processed.\n",
      "100900000 samples processed.\n",
      "101000000 samples processed.\n",
      "101100000 samples processed.\n",
      "101200000 samples processed.\n",
      "101300000 samples processed.\n",
      "101400000 samples processed.\n",
      "101500000 samples processed.\n",
      "101600000 samples processed.\n",
      "101700000 samples processed.\n",
      "101800000 samples processed.\n",
      "101900000 samples processed.\n",
      "102000000 samples processed.\n",
      "102100000 samples processed.\n",
      "102200000 samples processed.\n",
      "102300000 samples processed.\n",
      "102400000 samples processed.\n",
      "102500000 samples processed.\n",
      "102600000 samples processed.\n",
      "102700000 samples processed.\n",
      "102800000 samples processed.\n",
      "102900000 samples processed.\n",
      "103000000 samples processed.\n",
      "103100000 samples processed.\n",
      "103200000 samples processed.\n",
      "103300000 samples processed.\n",
      "103400000 samples processed.\n",
      "103500000 samples processed.\n",
      "103600000 samples processed.\n",
      "103700000 samples processed.\n",
      "103800000 samples processed.\n",
      "103900000 samples processed.\n",
      "104000000 samples processed.\n",
      "104100000 samples processed.\n",
      "104200000 samples processed.\n",
      "104300000 samples processed.\n",
      "104400000 samples processed.\n",
      "104500000 samples processed.\n",
      "104600000 samples processed.\n",
      "104700000 samples processed.\n",
      "104800000 samples processed.\n",
      "104900000 samples processed.\n",
      "105000000 samples processed.\n",
      "105100000 samples processed.\n",
      "105200000 samples processed.\n",
      "105300000 samples processed.\n",
      "105400000 samples processed.\n",
      "105500000 samples processed.\n",
      "105600000 samples processed.\n",
      "105700000 samples processed.\n",
      "105800000 samples processed.\n",
      "105900000 samples processed.\n",
      "106000000 samples processed.\n",
      "106100000 samples processed.\n",
      "106200000 samples processed.\n",
      "106300000 samples processed.\n",
      "106400000 samples processed.\n",
      "106500000 samples processed.\n",
      "106600000 samples processed.\n",
      "106700000 samples processed.\n",
      "106800000 samples processed.\n",
      "106900000 samples processed.\n",
      "107000000 samples processed.\n",
      "107100000 samples processed.\n",
      "107200000 samples processed.\n",
      "107300000 samples processed.\n",
      "107400000 samples processed.\n",
      "107500000 samples processed.\n",
      "107600000 samples processed.\n",
      "107700000 samples processed.\n",
      "107800000 samples processed.\n",
      "107900000 samples processed.\n",
      "108000000 samples processed.\n",
      "108100000 samples processed.\n",
      "108200000 samples processed.\n",
      "108300000 samples processed.\n",
      "108400000 samples processed.\n",
      "108500000 samples processed.\n",
      "108600000 samples processed.\n",
      "108700000 samples processed.\n",
      "108800000 samples processed.\n",
      "108900000 samples processed.\n",
      "109000000 samples processed.\n",
      "109100000 samples processed.\n",
      "109200000 samples processed.\n",
      "109300000 samples processed.\n",
      "109400000 samples processed.\n",
      "109500000 samples processed.\n",
      "109600000 samples processed.\n",
      "109700000 samples processed.\n",
      "109800000 samples processed.\n",
      "109900000 samples processed.\n",
      "110000000 samples processed.\n",
      "110100000 samples processed.\n",
      "110200000 samples processed.\n",
      "110300000 samples processed.\n",
      "110400000 samples processed.\n",
      "110500000 samples processed.\n",
      "110600000 samples processed.\n",
      "110700000 samples processed.\n",
      "110800000 samples processed.\n",
      "110900000 samples processed.\n",
      "111000000 samples processed.\n",
      "111100000 samples processed.\n",
      "111200000 samples processed.\n",
      "111300000 samples processed.\n",
      "111400000 samples processed.\n",
      "111500000 samples processed.\n",
      "111600000 samples processed.\n",
      "111700000 samples processed.\n",
      "111800000 samples processed.\n",
      "111900000 samples processed.\n",
      "112000000 samples processed.\n",
      "112100000 samples processed.\n",
      "112200000 samples processed.\n",
      "112300000 samples processed.\n",
      "112400000 samples processed.\n",
      "112500000 samples processed.\n",
      "112600000 samples processed.\n",
      "112700000 samples processed.\n",
      "112800000 samples processed.\n",
      "112900000 samples processed.\n",
      "113000000 samples processed.\n",
      "113100000 samples processed.\n",
      "113200000 samples processed.\n",
      "113300000 samples processed.\n",
      "113400000 samples processed.\n",
      "113500000 samples processed.\n",
      "113600000 samples processed.\n",
      "113700000 samples processed.\n",
      "113800000 samples processed.\n",
      "113900000 samples processed.\n",
      "114000000 samples processed.\n",
      "114100000 samples processed.\n",
      "114200000 samples processed.\n",
      "114300000 samples processed.\n",
      "114400000 samples processed.\n",
      "114500000 samples processed.\n",
      "114600000 samples processed.\n",
      "114700000 samples processed.\n",
      "114800000 samples processed.\n",
      "114900000 samples processed.\n",
      "115000000 samples processed.\n",
      "115100000 samples processed.\n",
      "115200000 samples processed.\n",
      "115300000 samples processed.\n",
      "115400000 samples processed.\n",
      "115500000 samples processed.\n",
      "115600000 samples processed.\n",
      "115700000 samples processed.\n",
      "115800000 samples processed.\n",
      "115900000 samples processed.\n",
      "116000000 samples processed.\n",
      "116100000 samples processed.\n",
      "116200000 samples processed.\n",
      "116300000 samples processed.\n",
      "116400000 samples processed.\n",
      "116500000 samples processed.\n",
      "116600000 samples processed.\n",
      "116700000 samples processed.\n",
      "116800000 samples processed.\n",
      "116900000 samples processed.\n",
      "117000000 samples processed.\n",
      "117100000 samples processed.\n",
      "117200000 samples processed.\n",
      "117300000 samples processed.\n",
      "117400000 samples processed.\n",
      "117500000 samples processed.\n",
      "117600000 samples processed.\n",
      "117700000 samples processed.\n",
      "117800000 samples processed.\n",
      "117900000 samples processed.\n",
      "118000000 samples processed.\n",
      "118100000 samples processed.\n",
      "118200000 samples processed.\n",
      "118300000 samples processed.\n",
      "118400000 samples processed.\n",
      "118500000 samples processed.\n",
      "118600000 samples processed.\n",
      "118700000 samples processed.\n",
      "118800000 samples processed.\n",
      "118900000 samples processed.\n",
      "119000000 samples processed.\n",
      "119100000 samples processed.\n",
      "119200000 samples processed.\n",
      "119300000 samples processed.\n",
      "119400000 samples processed.\n",
      "119500000 samples processed.\n",
      "119600000 samples processed.\n",
      "119700000 samples processed.\n",
      "119800000 samples processed.\n",
      "119900000 samples processed.\n",
      "120000000 samples processed.\n",
      "120100000 samples processed.\n",
      "120200000 samples processed.\n",
      "120300000 samples processed.\n",
      "120400000 samples processed.\n",
      "120500000 samples processed.\n",
      "120600000 samples processed.\n",
      "120700000 samples processed.\n",
      "120800000 samples processed.\n",
      "120900000 samples processed.\n",
      "121000000 samples processed.\n",
      "121100000 samples processed.\n",
      "121200000 samples processed.\n",
      "121300000 samples processed.\n",
      "121400000 samples processed.\n",
      "121500000 samples processed.\n",
      "121600000 samples processed.\n",
      "121700000 samples processed.\n",
      "121800000 samples processed.\n",
      "121900000 samples processed.\n",
      "122000000 samples processed.\n",
      "122100000 samples processed.\n",
      "122200000 samples processed.\n",
      "122300000 samples processed.\n",
      "122400000 samples processed.\n",
      "122500000 samples processed.\n",
      "122600000 samples processed.\n",
      "122700000 samples processed.\n",
      "122800000 samples processed.\n",
      "122900000 samples processed.\n",
      "123000000 samples processed.\n",
      "123100000 samples processed.\n",
      "123200000 samples processed.\n",
      "123300000 samples processed.\n",
      "123400000 samples processed.\n",
      "123500000 samples processed.\n",
      "123600000 samples processed.\n",
      "123700000 samples processed.\n",
      "123800000 samples processed.\n",
      "123900000 samples processed.\n",
      "124000000 samples processed.\n",
      "124100000 samples processed.\n",
      "124200000 samples processed.\n",
      "124300000 samples processed.\n",
      "124400000 samples processed.\n",
      "124500000 samples processed.\n",
      "124600000 samples processed.\n",
      "124700000 samples processed.\n",
      "124800000 samples processed.\n",
      "124900000 samples processed.\n",
      "125000000 samples processed.\n",
      "125100000 samples processed.\n",
      "125200000 samples processed.\n",
      "125300000 samples processed.\n",
      "125400000 samples processed.\n",
      "125500000 samples processed.\n",
      "125600000 samples processed.\n",
      "125700000 samples processed.\n",
      "125800000 samples processed.\n",
      "125900000 samples processed.\n",
      "126000000 samples processed.\n",
      "126100000 samples processed.\n",
      "126200000 samples processed.\n",
      "126300000 samples processed.\n",
      "126400000 samples processed.\n",
      "126500000 samples processed.\n",
      "126600000 samples processed.\n",
      "126700000 samples processed.\n",
      "126800000 samples processed.\n",
      "126900000 samples processed.\n",
      "127000000 samples processed.\n",
      "127100000 samples processed.\n",
      "127200000 samples processed.\n",
      "127300000 samples processed.\n",
      "127400000 samples processed.\n",
      "127500000 samples processed.\n",
      "127600000 samples processed.\n",
      "127700000 samples processed.\n",
      "127800000 samples processed.\n",
      "127900000 samples processed.\n",
      "128000000 samples processed.\n",
      "128100000 samples processed.\n",
      "128200000 samples processed.\n",
      "128300000 samples processed.\n",
      "128400000 samples processed.\n",
      "128500000 samples processed.\n",
      "128600000 samples processed.\n",
      "128700000 samples processed.\n",
      "128800000 samples processed.\n",
      "\n",
      "Finish writing ecg_sph_dataset.csv file...\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def get_arrhythmia_codes(dataframe):\n",
    "    # Returns the arrhythmia classification code for each file.\n",
    "    # Parameters:\n",
    "    #    dataframe (pandas.DataFrame): metadata with arrhythmia classification.\n",
    "    # Return:\n",
    "    #    Arrhythmia codes (dict).\n",
    "    #    ECG codes (dict).\n",
    "    codes_dict = {}\n",
    "    ecgs_dict = {}\n",
    "    for index, row in dataframe.iterrows():\n",
    "        arrhythmia_code = row[\"AHA_Code\"]\n",
    "        # Extract the primary statement without modifiers.\n",
    "        arrhythmia_code = arrhythmia_code.split(\";\")[0]\n",
    "        arrhythmia_code = arrhythmia_code.split(\"+\")[0]\n",
    "        if arrhythmia_code in codes_dict:\n",
    "            codes_dict[arrhythmia_code].append(row[\"ECG_ID\"])\n",
    "        else:\n",
    "            codes_dict[arrhythmia_code] = [row[\"ECG_ID\"]]\n",
    "        ecgs_dict[row[\"ECG_ID\"]] = arrhythmia_code\n",
    "    return codes_dict, ecgs_dict\n",
    "\n",
    "column_names = [\"idx\", \"ecg_id\", \"lead1\", \"lead2\", \"lead3\", \"aVR\", \"aVL\", \"aVF\", \"V1\", \"V2\", \"V3\", \"V4\", \"V5\", \"V6\", \"arrhythmia_code\"]\n",
    "number_of_steps = 5000\n",
    "csv_file_path = \"../dataset/csv_files/ecg_sph_dataset.csv\"\n",
    "\n",
    "# Load the h5 files list.\n",
    "files_list = glob.glob('../dataset/original_files/records/*.h5')\n",
    "print('Number of available files: {}'.format(len(files_list)))\n",
    "\n",
    "try:\n",
    "    print(\"\\nLoading metadata...\")\n",
    "    df_metadata = pd.read_csv(\"../dataset/original_files/metadata.csv\", sep=\",\")\n",
    "    arrhythmia_codes, ecg_codes = get_arrhythmia_codes(df_metadata)\n",
    "    if os.path.exists(csv_file_path):\n",
    "        os.remove(csv_file_path)\n",
    "    print(\"\\nStart writing ecg_sph_dataset.csv file...\\n\")\n",
    "    dict_idx = 0\n",
    "    with open(csv_file_path, \"w\", newline = '') as csvfile:\n",
    "        csvwriter = csv.DictWriter(csvfile, delimiter = '|', fieldnames = column_names)\n",
    "        csvwriter.writeheader()\n",
    "        for file_name in files_list:\n",
    "            ecg_id = os.path.basename(file_name).replace(\".h5\", \"\")\n",
    "            with h5py.File(file_name, 'r') as file:\n",
    "                signal = file['ecg'][()]\n",
    "                signal_matrix = np.zeros((number_of_steps, 12), dtype = np.float64)\n",
    "                for lead_index in range(0, 12):\n",
    "                    # Use the first 10 seconds (5000 samples) of each ECG.\n",
    "                    signals_array = signal[lead_index][0:number_of_steps]\n",
    "                    for line_index in range(0, number_of_steps):\n",
    "                        signal_matrix[line_index, lead_index] = signals_array[line_index]\n",
    "                for line_idx in range(0, number_of_steps):\n",
    "                    dict_aux = {}\n",
    "                    dict_aux[\"idx\"] = dict_idx\n",
    "                    dict_aux[\"ecg_id\"] = ecg_id\n",
    "                    for lead_id in range(2, 14):\n",
    "                        dict_aux[column_names[lead_id]] = signal_matrix[line_idx, lead_id - 2]\n",
    "                    dict_aux[\"arrhythmia_code\"] = int(ecg_codes[ecg_id])\n",
    "                    dict_idx += 1\n",
    "                    csvwriter.writerow(dict_aux)\n",
    "                    if dict_idx % 100000 == 0:\n",
    "                        print(\"{} samples processed.\".format(dict_idx))\n",
    "    print(\"\\nFinish writing ecg_sph_dataset.csv file...\")\n",
    "except Exception as e:\n",
    "    print(\"\\nFail to generate CSV file.\")\n",
    "    print(\"Error: {}\".format(e))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c43152d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
